00001 // Emacs Mode Line: -*- Mode:c++;-*- 00002 /* 00003 * Copyright (c) 2013 Battelle Memorial Institute 00004 * Licensed under modified BSD License. A copy of this license can be found 00005 * in the LICENSE file in the top level directory of this distribution. 00006 */ 00007 // ------------------------------------------------------------- 00008 /** 00009 * @file random.hpp 00010 * @author Bruce Palmer 00011 * @date 2015-03-03 09:25:03 d3g293 00012 * 00013 * @brief 00014 * This is a wrapper for a random number generator. The current implementation 00015 * relies on the standard random number generator in C++ and all the caveats 00016 * that apply to default random number generators should be noted. 00017 * 00018 */ 00019 00020 // ------------------------------------------------------------- 00021 00022 #ifndef _random_hpp_ 00023 #define _random_hpp_ 00024 00025 #include <cstdlib> 00026 #include <boost/random/linear_congruential.hpp> 00027 #include <boost/random/uniform_int.hpp> 00028 #include <boost/random/mersenne_twister.hpp> 00029 #include <boost/random/uniform_real.hpp> 00030 #include <boost/random/variate_generator.hpp> 00031 #include <boost/generator_iterator.hpp> 00032 00033 00034 typedef boost::mt19937 base_generator_type; 00035 //typedef boost::minstd_rand base_generator_type; 00036 00037 namespace gridpack { 00038 namespace random { 00039 00040 // ------------------------------------------------------------- 00041 // class GlobalIndexHashMap 00042 // ------------------------------------------------------------- 00043 class Random { 00044 public: 00045 00046 /** 00047 * Default constructor 00048 */ 00049 Random(); 00050 00051 /** 00052 * Initialize random number generator with a seed 00053 * @param seed random number generator initialization 00054 */ 00055 Random(int seed); 00056 00057 /** 00058 * Default destructor 00059 */ 00060 ~Random(void); 00061 00062 /** 00063 * Reinitialize random number generator with a new seed 00064 * @param seed random number generator initialization 00065 */ 00066 void seed(int seed); 00067 00068 /** 00069 * Return a double precision random number in the range [0,1] 00070 */ 00071 double drand(void); 00072 00073 /** 00074 * Return a double precision random number from a gaussian distribution with 00075 * unit variance 00076 */ 00077 double grand(void); 00078 00079 private: 00080 00081 int p_iset; 00082 double p_gset; 00083 double p_rand_max_i; 00084 00085 base_generator_type p_generator; 00086 boost::uniform_real<> p_uni_dist; 00087 boost::variate_generator<base_generator_type&, boost::uniform_real<> > p_uni; 00088 }; 00089 00090 00091 } // namespace random 00092 } // namespace gridpack 00093 00094 #endif 00095